import { Route, Redirect } from 'react-router-dom';

// 导入视图组件
import Login from '../pages/login';
import Dashboard from '../pages/dashboard';
import TeacherHome from '../pages/teacher/home';
import StudentHome from '../pages/student/home';
import ScoreManage from '../pages/score/manage';
import StudentManage from '../pages/student/manage';
import Mine from '../pages/mine';
import Layout from '../layouts';

// 引入仓库中的 state
import store from '../redux';

// 路由表
export const routes = [
    {
        path: '/login',
        component: Login,
    },
    {
        path: '/admin',
        // 父级公共组件
        component: Layout,
        children: [
            // 后台控制台（仪表盘）
            {
                icon: <i className="iconfont icon-kongzhitai"></i>,
                name: '控制台',
                path: '/admin/dashboard',
                component: Dashboard,
            },
            // 讲师首页
            {
                icon: <i className="iconfont icon-jiaolian1"></i>,
                name: '讲师首页',
                path: '/admin/teacher/home',
                component: TeacherHome,
                acl_id: '79897843-de4e-415c-a37b-500ebcb1a65b',
            },
            // 学生首页
            {
                icon: <i className="iconfont icon-xueshengtongji"></i>,
                name: '学生首页',
                path: '/admin/student/home',
                component: StudentHome,
                acl_id: '1b3c1adc-005d-4a09-ba7e-d6c63114d5a0',
            },
            // 成绩管理
            {
                icon: <i className="iconfont icon-chengji"></i>,
                name: '成绩管理',
                path: '/admin/score/manage',
                component: ScoreManage,
                acl_id: 'ff267fde-8d7a-4ae1-b190-030be91d3936',
            },
            // 学生管理
            {
                icon: <i className="iconfont icon-xueshengguanli"></i>,
                name: '学生管理',
                path: '/admin/student/manage',
                component: StudentManage,
                acl_id: '33677151-78a9-438f-b0a1-4cebc920027f',
            },
            // 我的页面
            {
                icon: <i className="iconfont icon-wode"></i>,
                name: '我的页面',
                path: '/admin/mine',
                component: Mine,
                acl_id: '55777c1c-6107-4133-ba1d-0483522de3c5',
            },
        ],
    },
];

// 封装的路由视图
export default ({ routes }) => {
    if (routes && routes.length !== 0) {
        return routes.map((item, index) => {
            return (
                <Route
                    key={index}
                    path={item.path}
                    render={(props) => {
                        // 登录了
                        if (
                            item.path === '/login' ||
                            // window.sessionStorage.getItem('token')
                            store.getState().token
                        ) {
                            return (
                                <item.component
                                    {...props}
                                    routes={item.children}
                                />
                            );
                        }
                        return <Redirect to="/login" />;
                    }}
                />
            );
        });
    }
    return null;
};
